/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JToggleButton;

import com.ruipengkj.bean.TProductCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonDishType;
import com.ruipengkj.component.jpanel.ImagePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.factory.UIFactory;
import com.ruipengkj.service.TProductCatalogService;
/**
 * 说明：
 * 菜品类型 面板
 */
public class OrderTypePanel3 extends ImagePanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	//菜品类型 service
	private TProductCatalogService tProductCatalogService = BuildSpringApplication.getInstance().getTProductCatalogServiceImpl();
	
	private PageView<TProductCatalog> pageView;//翻页主键
	private int currentPage = 1;
	
	private JButton prevButton;//上一个
	private JButton nextButton;//下一页
	
	public OrderTypePanel3() {
		Util.setSize(this, 40, 40);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setImage(ImageFactory.loadImage(ImageFactory.PANEL_DISH_TYPE_BG).getImage());
		init();
	}
	
	private void init() {
		prevButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV), this, false, false);
		nextButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT), this, false, false);
		
		pageView = new PageView<TProductCatalog>();
		pageView.setMaxResult(6);
		
		termQuery();
	}

	//条件查询
	protected void termQuery(){
		new Thread(){
			public void run(){
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TProductCatalog> pageModel = tProductCatalogService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult());
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						addButton(pageModel.getResultList());
					}else{
						//没有订单数据
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	private void addButton(List<TProductCatalog> tProductCatalogList) {
		this.removeAll();
		
		if (currentPage != 1)
			this.add(prevButton);
		ButtonGroup bg = new ButtonGroup();
		for (int i = 0, length = tProductCatalogList.size(); i < length && i < 6; ++i) {
			TProductCatalog tProductCatalog = tProductCatalogList.get(i);
			JToggleButton jb = new RPJButtonDishType(tProductCatalog.getName());
			jb.setName(tProductCatalog.getCatalogID());
//			jb.setSize(new Dimension(40, 40));
			jb.addActionListener(this);
			this.add(jb);
			bg.add(jb);
		}
		
		if (currentPage != pageView.getTotalPage())
			this.add(nextButton);
		this.updateUI();
	}
		
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(new Color(234, 99, 0));
		g.drawLine(0, 0, 0, this.getHeight());
		g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == nextButton) {
			if (currentPage < pageView.getTotalPage()) {
				++currentPage;
				termQuery();
			}
		} else if (e.getSource() == prevButton) {
			if (currentPage > 1) {
				--currentPage;
				termQuery();
			}
		} else {
			for (int i = this.getComponentCount() - 1; i >= 0; --i) {
				//点击了菜品分类按钮
				if (e.getSource() == this.getComponent(i)) {
					QueryDish(((JToggleButton)e.getSource()).getName());
					return;
				}
			}
		}
	}
	
	public void QueryDish(String CatalogId) {
	}
}
